{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Furthest Point From Origin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: furthestDistanceFromOrigin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #距离原点最远的点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code> 的字符串 <code>moves</code> ，该字符串仅由字符 <code>'L'</code>、<code>'R'</code> 和 <code>'_'</code> 组成。字符串表示你在一条原点为 <code>0</code> 的数轴上的若干次移动。</p>\n",
    "\n",
    "<p>你的初始位置就在原点（<code>0</code>），第 <code>i</code> 次移动过程中，你可以根据对应字符选择移动方向：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果 <code>moves[i] = 'L'</code> 或 <code>moves[i] = '_'</code> ，可以选择向左移动一个单位距离</li>\n",
    "\t<li>如果 <code>moves[i] = 'R'</code> 或 <code>moves[i] = '_'</code> ，可以选择向右移动一个单位距离</li>\n",
    "</ul>\n",
    "\n",
    "<p>移动 <code>n</code> 次之后，请你找出可以到达的距离原点 <strong>最远</strong> 的点，并返回 <strong>从原点到这一点的距离</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>moves = \"L_RL__R\"\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>可以到达的距离原点 0 最远的点是 -3 ，移动的序列为 \"LLRLLLR\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>moves = \"_R__LL_\"\n",
    "<strong>输出：</strong>5\n",
    "<strong>解释：</strong>可以到达的距离原点 0 最远的点是 -5 ，移动的序列为 \"LRLLLLL\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>moves = \"_______\"\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>可以到达的距离原点 0 最远的点是 7 ，移动的序列为 \"RRRRRRR\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= moves.length == n &lt;= 50</code></li>\n",
    "\t<li><code>moves</code> 仅由字符 <code>'L'</code>、<code>'R'</code> 和 <code>'_'</code> 组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [furthest-point-from-origin](https://leetcode.cn/problems/furthest-point-from-origin/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [furthest-point-from-origin](https://leetcode.cn/problems/furthest-point-from-origin/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"L_RL__R\"', '\"_R__LL_\"', '\"_______\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n=len(moves)\n",
    "        l= moves.count('L')\n",
    "        r= moves.count('R')\n",
    "        if l>=r:\n",
    "            return l-r+(n-l-r)\n",
    "        else:\n",
    "            return r-l+(n-l-r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R')-moves.count('L'))+moves.count('_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('L')-moves.count('R'))+moves.count('_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        point = 0\n",
    "        temp = 0\n",
    "        for i in range(len(moves)):\n",
    "            if moves[i] == \"L\": point -= 1\n",
    "            elif moves[i] == \"R\": point +=1\n",
    "            elif moves[i] == \"_\": temp += 1\n",
    "        return abs(point) + temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        if \"R\" not in moves and \"L\" not in moves:\n",
    "            return len(moves)\n",
    "        if moves.count(\"R\") >= moves.count(\"L\"):\n",
    "            moves = moves.replace(\"_\",\"R\")\n",
    "            return moves.count(\"R\") - moves.count(\"L\")\n",
    "        else:\n",
    "            moves = moves.replace(\"_\",\"L\")\n",
    "            return moves.count(\"L\") - moves.count(\"R\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        Left=moves.count('L')-moves.count('R')\n",
    "        Total_=len(moves)-moves.count('L')-moves.count('R')\n",
    "        if Left>=0:\n",
    "            return Left+Total_\n",
    "        else:\n",
    "            return -Left+Total_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R') - moves.count('L')) + moves.count('_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        lc,rc,_c = 0,0,0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                lc += 1\n",
    "            if i=='R':\n",
    "                rc += 1\n",
    "            if i=='_':\n",
    "                _c += 1\n",
    "        if lc>rc:\n",
    "            _c += lc-rc\n",
    "        else:\n",
    "            _c += rc-lc\n",
    "        return _c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        lcnt, rcnt, scnt = 0, 0, 0\n",
    "        for m in moves:\n",
    "            if m == 'L':\n",
    "                lcnt += 1\n",
    "            if m == 'R':\n",
    "                rcnt += 1\n",
    "            if m == '_':\n",
    "                scnt += 1\n",
    "        return abs(lcnt - rcnt) + scnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        a = 0  \n",
    "        for i in moves:  \n",
    "            if i == 'L' :  \n",
    "                a -= 1   \n",
    "            elif i=='R' :  \n",
    "                a += 1  \n",
    "            elif moves.count('L')>moves.count('R') :\n",
    "                a-=1\n",
    "            else:\n",
    "                a+=1\n",
    "        return abs(a)            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        arr1=['L' if i!='R' else i for i in moves]\n",
    "        arr2=['R' if i!='L' else i for i in moves]\n",
    "        ans=0\n",
    "        count=0\n",
    "        for i in arr1:\n",
    "            if i=='R':\n",
    "                count-=1\n",
    "            else:\n",
    "                count+=1\n",
    "        ans=max(ans,abs(count))\n",
    "        count=0\n",
    "        for i in arr2:\n",
    "            if i=='R':\n",
    "                count-=1\n",
    "            else:\n",
    "                count+=1\n",
    "        ans=max(ans,abs(count))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        a=moves.count(\"L\")        \n",
    "        b=moves.count(\"R\")\n",
    "        c=moves.count(\"_\")\n",
    "        if(a+c>b+c):\n",
    "            return a-b+c\n",
    "        return b-a+c\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "      l = moves.count(\"L\")\n",
    "      r = moves.count(\"R\")\n",
    "      both = moves.count(\"_\")\n",
    "      return abs(l-r) + both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        m, n, p = 0, 0, 0\n",
    "        for d in moves:\n",
    "            if d == 'L':\n",
    "                m += 1\n",
    "            elif d == 'R':\n",
    "                n += 1\n",
    "            else:\n",
    "                p += 1\n",
    "        return p + abs(m - n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        \"\"\"\n",
    "        return abs(moves.count(\"L\") - moves.count(\"R\")) + moves.count(\"_\") \n",
    "        \"\"\"\n",
    "        ds = 0\n",
    "        count_arbitary = 0\n",
    "        for i in moves:\n",
    "            if i == \"L\":\n",
    "                ds -= 1\n",
    "            elif i == \"R\":\n",
    "                ds += 1\n",
    "            else:\n",
    "                count_arbitary+=1\n",
    "        return abs(ds)+count_arbitary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R')-moves.count('L'))+moves.count('_')\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count(\"L\") - moves.count(\"R\")) + moves.count(\"_\") \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        cnt = Counter(moves)\n",
    "        return max(cnt.get('L', 0), cnt.get('R', 0))-min(cnt.get('L', 0), cnt.get('R', 0))+cnt.get('_', 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        distance = 0\n",
    "        num = 0\n",
    "        for i in moves:\n",
    "            if i == 'L':\n",
    "                distance -= 1\n",
    "            elif i == 'R':\n",
    "                distance += 1\n",
    "            else:\n",
    "                num += 1\n",
    "        distance = abs(distance) + num\n",
    "        return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n_r = moves.count('R')\n",
    "        n_l = moves.count('L')\n",
    "        n = len(moves)\n",
    "        n__ = n - n_l - n_r\n",
    "        return n__ + max(n_l - n_r, n_r - n_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        ans1,ans2=0,0\n",
    "        for x in moves:\n",
    "            if x == \"L\":\n",
    "                ans1-=1\n",
    "            else:\n",
    "                ans1+=1\n",
    "        for x in moves:\n",
    "            if x == \"R\":\n",
    "                ans2+=1\n",
    "            else:\n",
    "                ans2-=1\n",
    "        return max(abs(ans1),abs(ans2)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        lc, rc, zc = 0, 0, 0\n",
    "        for move in moves:\n",
    "            if move == \"L\":\n",
    "                lc += 1\n",
    "            elif move == \"R\":\n",
    "                rc += 1\n",
    "            else:\n",
    "                zc += 1\n",
    "        return lc + zc - rc if lc > rc else rc + zc - lc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        L=R=O=0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                L+=1\n",
    "            elif i=='R':\n",
    "                R+=1\n",
    "            else:\n",
    "                O+=1\n",
    "        return abs(L-R)+O\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n1 = 0\n",
    "        n2 = 0\n",
    "        for i in moves:\n",
    "            if i == 'R':\n",
    "                n1 -= 1\n",
    "            elif i == 'L':\n",
    "                n1 += 1\n",
    "            else:\n",
    "                n2 += 1\n",
    "        return abs(n1) + n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R')-moves.count('L'))+moves.count('_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R') - moves.count('L')) + moves.count('_')\n",
    "\n",
    "# 作者：灵茶山艾府\n",
    "# 链接：https://leetcode.cn/problems/furthest-point-from-origin/solutions/2413317/nao-jin-ji-zhuan-wan-yi-xing-dai-ma-by-e-yfn0/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l_num, r_num, x_num = moves.count('L'), moves.count('R'), moves.count('_')\n",
    "        if l_num - r_num > 0:\n",
    "            return (l_num - r_num + x_num)\n",
    "        else:\n",
    "            return (r_num - l_num + x_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        R=moves.count('R')\n",
    "        L=moves.count(\"L\")\n",
    "        return max(L,R)+len(moves)-L-R-min(L,R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        from collections import Counter\n",
    "        zd=Counter(moves)\n",
    "        return zd['_']+abs(zd['L']-zd['R'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R')-moves.count('L'))+moves.count('_')\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R') - moves.count('L')) + moves.count('_')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        a = moves.count('L') - moves.count('R')\n",
    "        if a>0 :\n",
    "            return a + moves.count('_')\n",
    "        else:\n",
    "            return abs(a - moves.count('_'))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        sum1=0\n",
    "        sum2=0\n",
    "        sum3=0\n",
    "        for move in moves:\n",
    "            if move==\"L\":\n",
    "                sum1+=1\n",
    "            elif move==\"R\":\n",
    "                sum2+=1\n",
    "            else:\n",
    "                sum3+=1\n",
    "        return abs(sum1-sum2)+sum3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, s: str) -> int:\n",
    "        l = s.count('L')\n",
    "        r = s.count('R')\n",
    "        t = s.count('_')\n",
    "        if l < r:\n",
    "            return r + t - l\n",
    "        return l + t - r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l = r = d = 0\n",
    "        for move in moves:\n",
    "            if move == 'L': l += 1\n",
    "            elif move == 'R': r += 1\n",
    "            else: d += 1\n",
    "        return abs(l - r) + d\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l,r=0,0\n",
    "        for i in moves:\n",
    "            if i==\"L\": l+=1\n",
    "            elif i==\"R\": r+=1\n",
    "        fr=moves.count(\"_\")\n",
    "        return max(l+fr-r,r+fr-l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('L') - moves.count('R')) + moves.count('_')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n,k=0,0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                n = n - 1\n",
    "            elif i=='R':\n",
    "                n = n + 1\n",
    "            else:\n",
    "                k=k+1\n",
    "        n = n-k if n<0 else n+k\n",
    "        return abs(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l=[0,0,0]\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                l[0]=l[0]+1\n",
    "            elif i=='R':\n",
    "                l[1]=l[1]+1\n",
    "            else:\n",
    "                l[2]=l[2]+1\n",
    "        return abs(l[0]-l[1])+l[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        r = moves.count(\"R\")\n",
    "        l = moves.count(\"L\")\n",
    "        d = moves.count(\"_\")\n",
    "        if r > l:\n",
    "            return r + d-l\n",
    "        else:\n",
    "            return l + d-r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        num_L = moves.count('L')\n",
    "        num_R = moves.count('R')\n",
    "        num__ = moves.count('_')\n",
    "        return abs(num_L - num_R) + num__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n=0\n",
    "        x=0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                n=n-1\n",
    "            elif i=='R':\n",
    "                n=n+1\n",
    "            elif i=='_':\n",
    "                x=x+1\n",
    "        print(n,x)\n",
    "        if  n==0:\n",
    "            return x\n",
    "        elif n>0:\n",
    "            return n+x\n",
    "        elif n<0:\n",
    "            return -(n-x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        R=moves.count('R')\n",
    "        L=moves.count(\"L\")\n",
    "        return max(L,R)+len(moves)-L-R-min(L,R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l=0\n",
    "        r=0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                l+=1\n",
    "            elif i=='R':\n",
    "                r+=1\n",
    "        return len(moves)+abs(l-r)-l-r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        movel = moves.count('L')\n",
    "        mover = moves.count('R')\n",
    "        moveany = moves.count('_')\n",
    "        return moveany + max(movel,mover) - min(movel,mover)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count(\"L\")  - moves.count(\"R\")) + moves.count(\"_\") \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        R=moves.count('R')\n",
    "        L=moves.count(\"L\")\n",
    "        return max(L,R)+len(moves)-L-R-min(L,R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count(\"R\") - moves.count(\"L\")) + moves.count(\"_\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R') - moves.count('L')) + moves.count('_')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        dic = {'L':0,'R':0,'_':0}\n",
    "        for i in moves:\n",
    "            dic[i] += 1\n",
    "        return max(dic['R']+dic['_']-dic['L'],dic['L']+dic['_']-dic['R'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l = r = d = 0\n",
    "        for move in moves:\n",
    "            if move == 'L': l += 1\n",
    "            elif move == 'R': r += 1\n",
    "            else: d += 1\n",
    "        return abs(l - r) + d\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R') - moves.count('L')) + moves.count('_')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        numL = 0\n",
    "        numR = 0\n",
    "        num = 0\n",
    "        for m in moves:\n",
    "            if m == 'L':\n",
    "                numL += 1\n",
    "            if m == 'R':\n",
    "                numR += 1\n",
    "            if m == '_':\n",
    "                num += 1\n",
    "        ans = max(numL,numR) - min(numL,numR) + num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        co=[]\n",
    "        for i in moves:\n",
    "            co.append(i)\n",
    "        a=co.count(\"L\")\n",
    "        b=co.count(\"R\")\n",
    "        c=co.count(\"_\")\n",
    "        if a>=b:\n",
    "            return a+c-b\n",
    "        if b>a:\n",
    "            return b+c-a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        return abs(moves.count('R')-moves.count('L'))+moves.count('_')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        res=Counter(moves)\n",
    "        if res['L']>res['R']:\n",
    "            return res['_']+res['L']-res['R']\n",
    "        elif res['L']<res['R']:\n",
    "            return res['_']+res['R']-res['L']\n",
    "        else:\n",
    "            return res['_']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        ret = 0\n",
    "        cnt = 0\n",
    "        for c in moves:\n",
    "            cnt += -1 if c=='L' else 1\n",
    "        ret = max(ret, cnt)\n",
    "        cnt = 0\n",
    "        for c in moves:\n",
    "            cnt += -1 if c=='R' else 1\n",
    "        ret = max(ret, cnt)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        ans=0\n",
    "        c=0\n",
    "        for i in moves:\n",
    "            if i=='L':\n",
    "                ans-=1\n",
    "            elif i=='R':\n",
    "                ans+=1\n",
    "            else:\n",
    "                c+=1\n",
    "        return abs(ans)+c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        l = 0\n",
    "        r = 0\n",
    "        _ = 0\n",
    "        for move in moves:\n",
    "            if move == 'L':\n",
    "                l += 1\n",
    "            elif move == 'R':\n",
    "                r += 1\n",
    "            else:\n",
    "                _ += 1\n",
    "        \n",
    "        return abs(l -r) + _\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        n=len(moves)\n",
    "        ans=0\n",
    "        l=moves.count('L')\n",
    "        r=moves.count('R')\n",
    "        t=moves.count('_')\n",
    "        ans=l*(-1)+r*1\n",
    "        if ans>=0:\n",
    "            ans=ans+t*1\n",
    "        else:\n",
    "            ans=ans+(-1)*t\n",
    "        print(ans)\n",
    "        return abs(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        cnt=Counter(moves)\n",
    "        return abs(cnt['L']-cnt['R'])+cnt['_']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "        x = 0        \n",
    "        a = moves.replace('_', 'L') if moves.count('L') >= moves.count('R') else moves.replace('_', 'R')\n",
    "        for i in a:\n",
    "            if i == 'L':\n",
    "                x -= 1\n",
    "            else:\n",
    "                x += 1\n",
    "        return abs(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def furthestDistanceFromOrigin(self, moves: str) -> int:\n",
    "\n",
    "        # 1. 使用Counter\n",
    "        # from collections import Counter\n",
    "\n",
    "        # ele_hashmap = Counter(moves)\n",
    "\n",
    "        # 2. 使用dic.get\n",
    "        # ele_hashmap = {}\n",
    "        # for step in moves:\n",
    "        #     ele_hashmap[step] = ele_hashmap.get(step, 0) + 1\n",
    "\n",
    "        # 3. 使用defaultdic\n",
    "        from collections import defaultdict\n",
    "\n",
    "        ele_hashmap = defaultdict(int)\n",
    "        for step in moves:\n",
    "            ele_hashmap[step] += 1\n",
    "\n",
    "        print(ele_hashmap)\n",
    "        selectable_step = ele_hashmap.get('_', 0)\n",
    "        max_step = max(ele_hashmap.get('L', 0), ele_hashmap.get('R', 0))\n",
    "        min_step = min(ele_hashmap.get('L', 0), ele_hashmap.get('R', 0))\n",
    "\n",
    "        return max_step - min_step + selectable_step"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
